
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;  

namespace test
{
	public class Application
	{
		static void Main (string[] args)
		{
			UIApplication.Main (args);
		}
	}

	// The name AppDelegate is referenced in the MainWindow.xib file.
	public partial class AppDelegate : UIApplicationDelegate
	{
		// This method is invoked when the application has loaded its UI and its ready to run
		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
		{
			// If you have defined a view, add it here:
			// window.AddSubview (navigationController.View);
			
			window.MakeKeyAndVisible ();
			
			Bonjour.NSNetService s = new Bonjour.NSNetService("", "_senk._tcp", "", 2000);
			s.Delegate = new MyNetServiceDelegate();
			s.Publish();
			
			Bonjour.NSNetServiceBrowser b = new Bonjour.NSNetServiceBrowser();
			b.Delegate = new MyNetServiceBrowserDelegate();
			b.SearchForServicesOfType(new NSString("_senk._tcp"), new NSString(""));
			
			return true;
		}

		// This method is required in iPhoneOS 3.0
		public override void OnActivated (UIApplication application)
		{
		}
	}
	
	public class MyNetServiceBrowserDelegate : Bonjour.NSNetServiceBrowserDelegate
	{
		public override void FoundService (Bonjour.NSNetServiceBrowser netServiceBrowser, Bonjour.NSNetService netService, bool moreServicesComing)
		{
			Console.WriteLine(netService); // This should print Bonjour.NSNetService, but it prints MonoTouch.Foundation.NSNetService
			Console.WriteLine(netServiceBrowser); // This is correct
			
			NSString name = (NSString)Runtime.GetNSObject(
			     Messaging.IntPtr_objc_msgSend(netService.Handle, new Selector("name").Handle)
			                    );
			Messaging.intptr_objc_msgSend_intptr(netService.Handle, new Selector("setDelegate:").Handle, new MyNetServiceDelegate().Handle);
			Messaging.IntPtr_objc_msgSend_Double(netService.Handle, new Selector("resolveWithTimeout:").Handle, 10);
			
			Console.WriteLine(name);
		}
		
		public override void SearchFailed (Bonjour.NSNetServiceBrowser netServiceBrowser, NSDictionary errorInfo)
		{
			NSNumber errorNumber = (NSNumber)errorInfo.ObjectForKey(new NSString("NSNetServicesErrorCode"));
			Bonjour.NSNetServicesError e = (Bonjour.NSNetServicesError)errorNumber.IntValue;
			//Console.WriteLine(errorCode.StringValue);
			Console.WriteLine(e.ToString());
		}

		public override void WillSearch (Bonjour.NSNetServiceBrowser netServiceBrowser)
		{
			Console.WriteLine("Will Search");
		}

	}
	
	public class MyNetServiceDelegate : Bonjour.NSNetServiceDelegate
	{
		public override void WillPublish(Bonjour.NSNetService sender)
		{
			Console.WriteLine("Will Publish");
		}
		
		public override void Published (Bonjour.NSNetService sender)
		{
			Console.WriteLine("Did Publish");
		}
		
		public override void ResolvedAddress (Bonjour.NSNetService sender)
		{
			Console.WriteLine("Address resolved.");
		}
		
		public override void WillResolve (Bonjour.NSNetService sender)
		{
			Console.WriteLine("Will resolve.");
		}

	}
}
